package ui 
{
	import Base.Loader.SWFLoader;
	import Base.Net.NetSocket;
	
	import Common.GameNetEvent;
	import Common.GameNetManager;
	import Common.RenderManager;
	
	import Events.LogicEvent;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import logic.GameCreatureData;
	import logic.GameData;

	/**
	 * ...
	 * @author ...
	 */
	public class PetUIHandler extends uiBase
	{
		public function PetUIHandler() 
		{
			path = "pet.swf";
			name = "PetUI";
		}
		
		
		public function updateData():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			uiMc.clear();
			
			var petArray:Array = GameManager.instance.MainPlayer.petArray;
			
			for ( var i:int = 0 ; i < petArray.length ; i++ )
			{
				var data:GameCreatureData = petArray[i].data;
				uiMc.addPet( data.Type , data.Name , data.Level , data.HP , data.MAXHP );
			}
		}
		
		public override function visible( b:Boolean ):void
		{
			super.visible( b );
			
			if ( b )
			{
				PetInfoUIHandler.instance.visible( false );
				PetInfoUIHandler.instance.visibleSkill( false );
			}
		}
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			for ( var i:int = 0 ; i < uiMc.petArray.length ; i++ )
			{
				uiMc.petArray[i].nameBtn.addEventListener( MouseEvent.CLICK , onClick( i ) );
				uiMc.petArray[i].rideBtn.addEventListener( MouseEvent.CLICK , onRide( i ) );
				uiMc.petArray[i].attackBtn.addEventListener( MouseEvent.CLICK , onAttack( i ) );
				uiMc.petArray[i].restBtn.addEventListener( MouseEvent.CLICK , onRest( i ) );
				uiMc.petArray[i].waitBtn.addEventListener( MouseEvent.CLICK , onWait( i ) );
			}
			
			uiMc.closeButton.addEventListener( MouseEvent.CLICK , onClose );
			
			updateData();
		}
		
		
		public function onClose( e:MouseEvent ):void
		{
			visible( false );
		}
		
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = uiMc.px + 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		public function sendType( i:int ):void
		{
			var petArray:Array = GameManager.instance.MainPlayer.petArray;
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_SET_PET_TYPE_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_SET_PET_TYPE_ID );
			
			netSocket.writeInt( petArray[ i ].ID );
			netSocket.writeInt( petArray[ i ].Type );
			
			netSocket.Send();
		}
		
		public function onRide( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var petArray:Array = GameManager.instance.MainPlayer.petArray;
				
				petArray[ i ].Type = GamePet.GPT_REST;
				uiMc.petArray[i].setType( petArray[ i ].Type );
				
				sendType( i );
			}
		}
		
		public function onAttack( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var petArray:Array = GameManager.instance.MainPlayer.petArray;
				
				petArray[ i ].Type = GamePet.GPT_REST;
				uiMc.petArray[i].setType( petArray[ i ].Type );
				
				sendType( i );
				
				GameManager.instance.MainPlayer.attackPet = null;
			}
		}
		
		public function onRest( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var petArray:Array = GameManager.instance.MainPlayer.petArray;
				
				petArray[ i ].Type = GamePet.GPT_WAIT;
				uiMc.petArray[i].setType( petArray[ i ].Type );
				
				sendType( i );
			}
		}
		
		public function onWait( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				var petArray:Array = GameManager.instance.MainPlayer.petArray;
				
				for ( var j:int = 0 ; j < petArray.length ; j++ )
				{
					if ( petArray[ j ].Type == GamePet.GPT_ATTACK )
					{
						petArray[ i ].Type = GamePet.GPT_REST;
						uiMc.petArray[ i ].setType( petArray[ i ].Type );
						
						sendType( i );
						
						return;
					}
				}
				
				petArray[ i ].Type = GamePet.GPT_ATTACK;
				uiMc.petArray[i].setType( petArray[ i ].Type );
				
				sendType( i );
				
				GameManager.instance.MainPlayer.attackPet = petArray[ i ];
			}
		}
		
		
		public function onClick( i:int ):Function
		{
			return function foo( e:MouseEvent ):void
			{
				PetInfoUIHandler.instance.show( i );
				visible( false );
			}
		}
		
		public static var instance:PetUIHandler = null;
		public static function Instance():PetUIHandler
		{
			if ( instance == null )
			{
				instance = new PetUIHandler();
			}
			
			return instance;
		}
	}

}